﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using Jungle.Code.Utils;

namespace Jungle.Code.Core
{
    public class FormBuilder
    {
        #region Fields and Properties
        public System.Web.UI.WebControls.Table table;
        private List<InputField> fieldList = new List<InputField>();


        private struct InputField
        {
            public InputField(FieldType t, string id, string val)
            {
                this.type = t;
                this.ID = id;
                this.value = val;
            }
            public FieldType type;
            public string ID;
            public string value;
        }

        #endregion

        #region Public and Static methods
        public FormBuilder()
        {
            table = new System.Web.UI.WebControls.Table();
            table.CssClass = "fob-table";
        }

        public void AddTextBox(string ID, string title, string value, bool required, InputType type, bool validation)
        {
            fieldList.Add(new InputField(FieldType.Text, ID, value));
            TableRow tr = new TableRow();
            TableCell tc1 = new TableCell();
            tc1.CssClass = "l";
            TableCell tc2 = new TableCell();
            tc2.CssClass = "fr";

            Label lb = new Label();
            lb.Text = title;
            tc1.Controls.Add(lb);

            TextBox tb = new TextBox();
            tb.Text = value;
            tb.ID = ID;
            tc2.Controls.Add(tb);
            /*Required*/
            if (required)
            {
                RequiredFieldValidator rfv = new RequiredFieldValidator();
                rfv.ControlToValidate = ID;
                rfv.Display = ValidatorDisplay.Dynamic; rfv.ErrorMessage = "Поле надо заполнить";
                tc2.Controls.Add(rfv);
            }
            if (validation)
            {
                RegularExpressionValidator rev = new RegularExpressionValidator();
                if (type == InputType.Int)
                {
                    rev.ValidationExpression = Resources.ValidationExpression.Int;
                    rev.ErrorMessage = "Введите целое число";
                }
                rev.Display = ValidatorDisplay.Dynamic;
                rev.ControlToValidate = ID;
                tc2.Controls.Add(rev);
            }


            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);
            table.Rows.Add(tr);
        }

        public void AddDropDownList(string ID, string title, string value, ListItem[] items)
        {
            fieldList.Add(new InputField(FieldType.DropDownList, ID, value));
            TableRow tr = new TableRow();
            TableCell tcLeft = new TableCell();
            tcLeft.CssClass = "l";
            TableCell tcRight = new TableCell();
            tcRight.CssClass = "fr";
            tr.Cells.Add(tcLeft);
            tr.Cells.Add(tcRight);
            //fill tcLeft
            Label lb = new Label();
            lb.Text = title;
            tcLeft.Controls.Add(lb);
            //fill tcRight
            DropDownList ddl = new DropDownList();
            ddl.ID = ID;
            ddl.Items.AddRange(items);
            if (ddl.Items.FindByValue(value) != null)
                ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));
            tcRight.Controls.Add(ddl);
            table.Rows.Add(tr);
        }

        public void AddCheckBox(string ID, string title, bool check)
        {
            fieldList.Add(new InputField(FieldType.CheckBoxs, ID, check.ToString()));
            TableRow tr = new TableRow();
            TableCell tcLeft = new TableCell();
            tcLeft.CssClass = "l";
            TableCell tcRight = new TableCell();
            tcRight.CssClass = "fr";
            tr.Cells.Add(tcLeft);
            tr.Cells.Add(tcRight);
            //fill tcLeft
            Label lb = new Label();
            lb.Text = title;
            tcLeft.Controls.Add(lb);
            //fill tcRight
            CheckBox chk = new CheckBox();
            chk.ID = ID;
            chk.Checked = check;
            tcRight.Controls.Add(chk);
            table.Rows.Add(tr);
        }

        public void AddImage(string ID, string title, string url)
        {
            fieldList.Add(new InputField(FieldType.Image, ID, url));

            TableRow tr = new TableRow();
            TableCell tc1 = new TableCell();
            tc1.CssClass = "l";
            TableCell tc2 = new TableCell();
            tc2.CssClass = "fr";
            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);
            //
            Label lb = new Label();
            lb.Text = title;
            tc1.Controls.Add(lb);
            //
            FileUpload fup = new FileUpload();
            fup.ID = ID;
            tc2.Controls.Add(fup);

            if (url != String.Empty)
            {
                Image img = new Image();
                img.ImageUrl = UrlHelper.GetSiteUrl() + url;
                tc2.Controls.Add(img);
            }

            table.Rows.Add(tr);
        }

        public void AddHtml(string text)
        {
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tr.Cells.Add(tc);

            tc.CssClass = "l";
            tc.ColumnSpan = 2;
            Label lb = new Label();
            lb.Text = text;
            tc.Controls.Add(lb);
            table.Rows.Add(tr);

        }

        public string Get(string ID)
        {
            InputField field = new InputField();
            foreach (InputField field2 in this.fieldList)
            {
                if (field2.ID == ID)
                {
                    field = field2;
                    break;
                }
            }
            if (field.ID == ID)
            {
                switch (field.type)
                {
                    case FieldType.Text:
                        {
                            TextBox box = (TextBox)this.table.FindControl(field.ID);
                            return box.Text;
                        }
                    case FieldType.DropDownList:
                        {
                            DropDownList list = (DropDownList)this.table.FindControl(field.ID);
                            if (list == null)
                            {
                                break;
                            }
                            return list.SelectedValue;
                        }
                    case FieldType.CheckBoxs:
                        {
                            CheckBox box2 = (CheckBox)this.table.FindControl(field.ID);
                            if (box2 == null)
                            {
                                break;
                            }
                            return box2.Checked.ToString();
                        }
                    case FieldType.Image:
                        {
                            FileUpload upload = (FileUpload)this.table.FindControl(field.ID);
                            if (!upload.HasFile)
                            {
                                break;
                            }
                            return upload.PostedFile.FileName;
                        }
                }
            }
            return string.Empty;
        }

 

 

        #endregion
    }

    public enum InputType
    {
        Int,
        Float,
        Date,
        DateTime,
        Text,
    }

    public enum FieldType
    {
        Text = 1,
        VisionEditor = 2,
        DropDownList = 3,
        CheckBoxs = 4,
        Image = 5
    }
}
